package com.example.hewei.smarttransport.Request;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 何伟 on 2017/3/7.
 */

public abstract class TrafficRequest extends BaseRequest {
    // 存放服务器端的回应结果（成功或失败），基类只提供读取接口，具体内容由子类提供
    protected volatile boolean isSuccess = false;
    // 用户名
    protected String userName = "";

    /**
     * 构造函数
     */
    public TrafficRequest(String userName) {
        super();
        this.userName = userName;
    }

    /**
     * 设置用户名
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * 标记本次请求是否成功
     *
     * @return 是否成功
     */
    public boolean isSuccess() {
        return isSuccess;
    }

    /**
     * JSON数据解析
     */
    @Override
    protected void onJasonParese(String responseStr) {
        try {
//			Log.e("回传",responseStr);
            JSONObject jsonRespObj = new JSONObject(responseStr);
            if (jsonRespObj != null) {
                if (jsonRespObj.has("result")) {
                    String result = jsonRespObj.getString("result");
                    // “result”键值为"ok"则成功，否则失败
                    if (result.equals("ok")) {
                        isSuccess = true;
                    } else {
                        isSuccess = false;
                    }
                }
            }
        } catch (JSONException e) {
            isSuccess = false;
            e.printStackTrace();
        }
    }
}

